#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<string>
#include<string.h>
#include<assert.h>
namespace xsc
{
    template <class T>
    class vector
    {
    public:
        typedef T* iterator;
        vector()
            :_start(nullptr)
            , _finish(nullptr)
            , _end_of_storage(nullptr)
        {}

        const iterator begin()
        {
            return _start;
        }

        const iterator end()
        {
            return _finish;
        }

        template<class InputIterator>
        vector(InputIterator first, InputIterator last)
            :_start(nullptr)
            , _finish(nullptr)
            , _end_of_storage(nullptr)
        {
            while (first != last)
            {
                push_back(*first);
                ++first;
            }
        }

        void swap(vector<T>& v)
        {
            std::swap(_start, v._start);
            std::swap(_finish, v._finish);
            std::swap(_end_of_storage, v._end_of_storage);
        }

        size_t size()
        {
            return _finish - _start;
        }

        size_t capacity() const
        {
            return _end_of_storage - _start;
        }

        T& operator[](size_t pos)
        {
            return _start[pos];
        }


        vector(vector<T>& v)
            :_start(nullptr)
            , _finish(nullptr)
            , _end_of_storage(nullptr)
        {
            vector<T> tmp(v.begin(), v.end());
            swap(tmp);
        }

        vector<T>& operator=(vector<T> v)
        {
            swap(v);
            return *this;
        }

        void reserve(size_t n)
        {
            if (n > capacity())
            {
                T* tmp = new T[n];
                size_t sz = size();
                //此处拷贝不能使用memcpy，如果是string类型则为浅拷贝，使用=赋值运算符是深拷贝；
                if (_start)
                {
                    for (size_t i = 0; i < sz; i++)
                    {
                        tmp[i] = _start[i];
                    }
                    delete[] _start;
                }
                _start = tmp;
                _finish = _start + sz;
                _end_of_storage = _start + n;
            }
        }

        void resize(size_t n, T val = T())
        {
            if (n <= size())
            {
                _finish = _start + n;
            }
            else
            {
                if (n > capacity())
                {
                    reserve(n);
                }
                while (_finish != _end_of_storage)
                {
                    *_finish = val;
                    ++_finish;
                }
            }
        }

        void push_back(const T& x)
        {
            if (_finish == _end_of_storage)
            {
                int newcapacity = capacity() == 0 ? 4 : capacity() * 2;
                reserve(newcapacity);
            }
            *_finish = x;
            ++_finish;
        }

        void insert(size_t pos, const T& x)
        {
            assert(pos <= size());
            if (_finish == _end_of_storage)
            {
                size_t  newcapacity = capacity() == 0 ? 4 : capacity() * 2;
                reserve(newcapacity);
            }
            size_t mvsz = _finish - _start + pos;
            memmove(_start + pos + 1, _start + pos, mvsz * sizeof(T));
            *(_start + pos) = x;
            ++_finish;
        }

        void erase(size_t pos)
        {
            assert(pos < size());
            size_t mvsz = _finish - _start + pos - 1;
            memmove(_start + pos, _start + pos + 1, mvsz * sizeof(T));
            --_finish;
        }

        ~vector()
        {
            delete[] _start;
        }

    private:
        iterator _start;
        iterator _finish;
        iterator _end_of_storage;
    };

    void test()
    {
        vector<string> s;
        s.push_back("1111111111");
        s.push_back("111111111111");
        s.push_back("11111111111");
        s.push_back("1111111111111");
        s.push_back("11111111111111");
        int i = 0;
        while (i < s.size())
        {
            cout << s[i] << " ";
            i++;
        }
        cout << endl;

    }

}


int main()
{
    //xsc::test();
   
    return 0;
}
